/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    void comp(TreeNode* root, TreeNode* subRoot, bool &mark) {
        if (mark == false) return;
        if (root || subRoot) {
            if (!(root && subRoot && root->val == subRoot->val)) {
                mark = false;
                return;
            }
        }
        else {
            return;
        }
        comp(root->left, subRoot->left, mark);
        comp(root->right, subRoot->right, mark);
    }
public:
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        queue<TreeNode*> qt;
        qt.emplace(root);
        TreeNode* node = nullptr;
        while (!qt.empty()) {
            node = qt.front();
            if (node->val == subRoot->val) {
                bool mark = true;
                comp (node, subRoot, mark);
                if (mark) return true;
            } 
            qt.pop();
            if (node->left) qt.emplace(node->left);
            if (node->right) qt.emplace(node->right);
        }
        return false;
    }
};